package com.ymttest.database.sqlwapper;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name : YmtIdCardInfoWapper.java
 * @Author : sunsheng
 * @Date : 2015年10月28日
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtIdCardInfoWapper {
	SqlManager manage;

	public YmtIdCardInfoWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());
	}

	/**
	 * insert 一条YmtIdCard记录
	 * 
	 * @param record
	 *            insertProducts
	 * @return
	 * @throws InterruptedException
	 */
	public int insertIdCardInfo(YmtIdCardInfo record) {
		int i = manage.insert(
				"com.ymttest.database.mapping.YmtIdCardInfoMapper.insert",
				record);
		manage.commit();
		// manage.close();
		return i;
	}
	/**
	 * 根據BuyerId 查詢身份證信息
	 * 
	 * @param buyerId
	 * @return
	 * @throws InterruptedException
	 */
	public YmtIdCardInfo selectByBuyerId(long buyerId) {

		Map m = new HashedMap();
		m.put("buyerId", buyerId);
		m.put("isDelete", 1);
		m.put("orderByClause", "UploadTime desc");

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						m);

		if (CollectionUtils.isEmpty(result))
			return null;

		return result.get(0);

	}
	/**
	 * 根据BuyerId 查询身份信息
	 * 
	 * @param cardId
	 * @return
	 * @throws InterruptedException
	 */
	public List<YmtIdCardInfo> selectByBuyerIdList(long buyerId) {

		Map m = new HashedMap();
		m.put("buyerId", buyerId);
		m.put("isDelete", 1);

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						m);

		if (CollectionUtils.isEmpty(result))
			return null;

		return result;

	}

	/**
	 * 根据name,idNumber 查询身份信息
	 * 
	 * @param cardId
	 * @return
	 * @throws InterruptedException
	 */
	public YmtIdCardInfo selectByNameAndId(String name, String cardNumber) {

		Map m = new HashedMap();

		m.put("name", name);
		m.put("cardNumber", cardNumber);

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						m);

		if (CollectionUtils.isEmpty(result))
			return null;

		return result.get(0);

	}

	/**
	 * 根据idNumber 查询身份信息
	 * 
	 * @param cardId
	 * @return
	 * @throws InterruptedException
	 */
	public List<YmtIdCardInfo> selectByCardId(String cardNumber) {

		Map m = new HashedMap();

		m.put("cardNumber", cardNumber);

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						m);
		return result;
	}

	/**
	 * 根據name,buyerId 查詢身份證信息
	 * 
	 * @param cardId
	 * @return
	 * @throws InterruptedException
	 */
	public YmtIdCardInfo selectByNameAndBuyerId(String name, long buyerId) {

		Map m = new HashedMap();

		m.put("name", name);
		m.put("buyerId", buyerId);
		m.put("isDelete", 1);

		List<YmtIdCardInfo> result = manage
				.getSelectResults(
						"com.ymttest.database.mapping.YmtIdCardInfoMapper.selectByExample",
						m);

		if (CollectionUtils.isEmpty(result))
			return null;

		return result.get(0);

	}

	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) {

		System.out.println(new YmtIdCardInfoWapper().selectByBuyerId(1));

	}
}
